<template>
  <div class="ulbox" v-if="detail?.id">
    <div class="libox">
      <my-item :detail="detail" @handleClose="handleWebSocket"></my-item>
    </div>
  </div>
  <van-empty
    v-else
    description="暂无数据"
    image-size="100"
    :image="require('@/assets/images/null.png')"
  />
</template>
    
<script setup lang='ts'>
import MyItem from "./MyItem.vue";
import { useStore } from "@/store";
import { ref, onMounted, defineExpose, computed, onUnmounted } from "vue";
import http from "@/utils/http";
import { Toast } from "vant";
import { SOCKET_URL } from "@/utils/common";
const store = useStore();
const infos = store.state.users.infos;
const detail = ref<any>({});
// onMounted(() => {
//   console.log('刷新2')
//   getAiList();
// });
let websocket: any;
const token = computed(() => store.state.users.token);
onMounted(() => {
  handleWebSocket();
});
onUnmounted(() => {
  websocket && websocket.close();
});
const handleWebSocket = () => {
  websocket && websocket.close();
  websocket = new WebSocket(
    `${SOCKET_URL}/strategy/ws/info?access_token=${token.value}`
  );
  websocket.onopen = () => {
    websocket.send(
      JSON.stringify({
        type: "subscribe",
        params: {
          requestApi: "strategy_contract_current_detail",
          requestParam: {},
        },
      })
    );
  };
  websocket.onmessage = (data: any) => {
    if (data.data) {
      detail.value = JSON.parse(data.data);
    }
  };
};
// const getAiList = () => {
//     Toast.loading({
//         message: '加载中...',
//         forbidClick: true,
//         duration: 0,
//       });
//   http.get("strategy/app/contract/current").then((res: any) => {
//     Toast.clear()
//     if (res.code === 0) {
//       detail.value = res.data;
//     }
//   });
// };
// defineExpose({
//   getAiList
// });
</script>
    
<style lang="scss" scoped>
</style>